var express = require('express');
var router = express.Router();
var md5 = require("md5");

var usersDAO = require("../bin/db/usersDAO.js");

var isLogin = require("../bin/isLogin.js");

router.post("/regist",(req,res)=>{
  console.log(req.body);
  usersDAO.findOne({username:req.body.username})
  .then(data=>{
    if(data){
      res.json({err:0,msg:"该用户名已存在"})
    }else{
      var u = new usersDAO(req.body);
      u.psw = md5(u.psw);
      u.save()
      .then(data=>{
        res.json({err:0,msg:"注册成功"});
      });
    }
  })

});
// 
router.post("/login",(req,res)=>{
  req.body.psw = md5(req.body.psw)
  usersDAO.findOne(req.body)
  .then(data=>{
    if(data){
      req.session.user = data;
      res.json({err:0,msg:"登陆成功"});
    }else{
      res.json({err:1,msg:"用户名或密码错误不正确"})
    }
    
  })
});
// 
router.post("/islogin",(req,res)=>{
  if(req.session.user){
    res.json({err:0,userInfo:req.session.user});
  }else{
    res.json({err:1})
  }
});
// 退出
router.post("/exit",(req,res)=>{
  req.session.user = null;
  res.json({err:0,msg:"退出成功"});
});

// 卖家
router.post("/seller",isLogin);
router.post("/seller",(req,res)=>{

    usersDAO.findOne({username:req.session.user.username})
  .then(data=>{
    if(data){
      if(data.isSeller){
        res.json({err:1,msg:"你已经是卖家了！"});
      }else{

          data.nameOfShop=req.body.nameOfShop;
          data.nameDesp=req.body.nameDesp;
          data.isSeller=true;
        data.save()
        .then(data=>{
          res.json({msg:"恭喜你！成为卖家"});
        });
      }
    }
  });
});

// 是否登陆
router.post("/isSeller",isLogin);
router.post("/isSeller",(req,res)=>{
  usersDAO.findOne({username:req.session.user.username})

.then(data=>{
  if(data.isSeller){
    req.session.user = data;
    res.json({err:0,isSeller:true});
  }else{
    res.json({err:0,isSeller:false});
  }
})
});

// 加入购物车
router.post("/addcart",isLogin);
router.post("/addcart",(req,res)=>{
  console.log(req.body.id)
  usersDAO.findOne({_id:req.session.user._id})
  .then(data=>{
    var isHas = false;
    var j;
    data.shoppingCart.forEach(function(v,i){
      if(v==req.body.id){
        isHas = true;
        j = i;
      }
    },this);
    if(isHas){
      var sum = data.count[j];
      sum++;
      data.count.splice(j,1,sum);
    }
    if(!isHas){
      data.shoppingCart.push(req.body.id);
      data.count.push(1);
    }
    data.save()
    .then(()=>{
      res.json({err:0,msg:"添加成功"});
    })
  })
});

// 购物车页面接口
router.get("/shoppingcart",isLogin);
router.get("/shoppingcart",(req,res)=>{
  usersDAO.findOne({_id:req.session.user._id},{_id:1,count:1,shoppingCart:1})
  .populate("shoppingCart")
  .then(data=>{
    res.json({err:0,data})
  })
});

// 购物车数量改变接口
router.post("/count",isLogin);
router.post("/count",(req,res)=>{
  console.log(req.body)
  usersDAO.findOne({_id:req.session.user._id})
  .then(data=>{
    var c;
    data.shoppingCart.forEach(function(v,i){
      if(v==req.body.id){
        c = i;
      }
    },this);
    var count = data.count[c];
    if(req.body.change){
      count++;
    }else{
      if(count<=1){
        count = 1;
      }else{
        count--;
      }
    }
    data.count.splice(c,1,count);
    data.save()
    .then(data=>{
      console.log(data.count)
      res.json({err:0})
    })
  });
});

// 删除购物车选中商品
router.get("/count",isLogin);
router.get("/delete",(req,res)=>{
  usersDAO.findOne({_id:req.session.user._id})
  .then(data=>{
    data.shoppingCart.forEach(function(e,i){
      req.query.checkId.forEach(function(v){
        if(e==v){
           data.shoppingCart.splice(i,1);
           data.count.splice(i,1);
        }
      },this)
    },this)
    data.save()
    .then(data=>{
      res.json({err:0,msg:"删除成功"})
    })
  })
})
module.exports = router;
